/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: directory_posix.cpp
 *
 * Purpose: directory posix implmentation
 *
 * Developer:
 *   wen.gu , 2019-10-15
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "panda/core/directory.h"

#if defined(HAVE_POSIX_DIR_API) && HAVE_POSIX_DIR_API
  
#include <dirent.h>

namespace panda
{
namespace core
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#ifndef F_OK
#define F_OK 0
#endif
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
class Directory::impl
{
public:
    DIR* mDirHdl = nullptr;
    std::string mCurDir = "";
    bool mIsDir = false;
public:
    const std::string& next()
    {
        do
        {
            struct dirent* pDir = readdir(mDirHdl);
            if (pDir)
            {
                mCurDir = pDir->d_name;
                mIsDir = (pDir->d_type == DT_DIR);
            }                
            else
            {
                mCurDir.clear();
            }
                
        } while ((mCurDir == ".") || (mCurDir == ".."));

        return mCurDir;
    }
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

std::shared_ptr<Directory> Directory::OpenDir(const std::string& dir)
{
    std::shared_ptr<Directory> pDir = nullptr;

    if (Directory::IsAccessale(dir))
    {
        pDir = std::make_shared<Directory>();
        pDir->mImpl->mDirHdl = opendir(dir.c_str());

        if (pDir->mImpl->mDirHdl)
        {
            pDir->mImpl->next();
        }
        else
        {
            pDir = nullptr;
        }
    }

    return pDir;
}

//static 
bool Directory::IsAccessale(const std::string& path)
{
    return (access(path.c_str(), F_OK) == 0);
}


Directory::~Directory()
{
    /** todo something */
    if (mImpl->mDirHdl)
    {
        closedir(mImpl->mDirHdl);
        mImpl->mDirHdl = nullptr;
    }
}


Directory::Directory()
    :mImpl(new impl)
{
    /** todo something */
}


/** isDir: true: directory, false: file */
bool Directory::foreach(void(*dirFunc)(std::string& path, bool isDir, void* opaque), void* opaque)
{
    if (dirFunc)
    {
        std::string& curDir = mImpl->mCurrentDir;
        bool& isDir = mImpl->mCurIsDir;

        while (curDir.empty != false)
        {
            dirFunc(curDir, isDir, opaque);
            mImpl->next();
        }

        return true;
    }

    return false;
}

} /** namespace core */
} /** namespace panda */

#endif /** defined HAVE_POSIX_DIR_API */
